
require "makeup/makeUpBlendMode.lua"
require "makeup/beautyStrength.lua"
require "makeup/appStrength.lua"
require "makeup/lipLayer.lua"
require "makeup/config.lua"

makeup = nil
eyeLinerPath = nil
oldEyeShadowLayer1BlendMode = nil
oldEyeShadowLayer2BlendMode = nil


oldLipLayerBlendModes = {nil, nil, nil}
lipLayers = {nil, nil, nil}

function makeupinitialize(scene)
  makeup = KuruMakeupNode.create(true)
  scene:addNodeAndRelease(makeup)

  makeup:setPathAndBlendMode(KuruMakeupNodeType.BLUSH, BASE_DIRECTORY .. "makeup/img/blush.png", blushBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.CONTOUR, BASE_DIRECTORY .. "makeup/img/contour.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.LIP_COLOR, BASE_DIRECTORY .. "makeup/img/lip_color.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYE_COLOR, BASE_DIRECTORY .. "makeup/img/eye_color_0001/eye_color_0001.gpb", eyeColorBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYEBROWS, BASE_DIRECTORY .. "makeup/img/eyebrows.png", eyeBrowsBlendMode)

  --eyeShadow
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER1, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L1.png", eyeShadowLayer1BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER2, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L2.png", eyeShadowLayer2BlendMode)

  --eyeLiner

  eyeLinerPath = BASE_DIRECTORY .. "makeup/img/eye_liner/eye_liner.png"
  if (DeviceConfig.instance().platformType == DeviceConfigPlatformType.P_IOS) then
    local eyeLinerPath_iOS = BASE_DIRECTORY .. "makeup/img/eye_liner_iOS/eye_liner_iOS.png"
    local file = io.open(eyeLinerPath_iOS, "r")
    if file ~= nil then
      eyeLinerPath = eyeLinerPath_iOS
    end
  end

  print("eyeLinerPath = " .. eyeLinerPath)

  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELINER, eyeLinerPath, eyeLinerBlendMode)

  --eyeLashes
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELASHES, BASE_DIRECTORY .. "makeup/img/eyelashes/eyelashes.png", eyeLashesBlendMode)

  makeup_param = makeup:getParam()

  -- beauty
  makeup_param.laughLine = laughLine
  makeup_param.eyeLut = eyeLut
  makeup_param.eyePlim = eyePlim
  makeup_param.darkCircle = darkCircle
  makeup_param.whiteTeeth = whiteTeeth

  -- makeup
  makeup_param.faceContour = faceContour
  makeup_param.cheek = cheek
  makeup_param.lip = lip
  makeup_param.colorLens = colorLens
  makeup_param.eyeBrow = eyeBrow
  makeup_param.eyeShadow = eyeShadow
  makeup_param.eyeLiner = eyeLiner
  makeup_param.eyeLashes = eyeLashes

  EngineStatus.instance():setBoolean("useBuiltInDistortionInScript", true)
  -- meshDistortionNode = KuruMeshDistortionNode.create()
  -- meshDistortionNode:loadDistortionFromString(SceneConfig.instance().distortionJson)
  -- scene:addNodeAndRelease(meshDistortionNode)

  distortionNode = KaleFaceDistortionNode.create()
  distortionNode:setMeshDistortionNode(meshDistortionNode)
  distortionNode:loadDistortionFromString(SceneConfig.instance().distortionJson)
  scene:addNodeAndRelease(distortionNode)

  -- if PropertyConfig.instance():getBoolean("meshContourOn", false) then
  --   meshContourNode = KuruMeshContourDistortionNode.create()
  --   meshContourNode:setMeshDistortionNode(meshDistortionNode)
  --   meshContourNode:loadDistortionFromString(PropertyConfig.instance():getString("meshContourJson", ""))
  --   scene:addNodeAndRelease(meshContourNode)
  -- end
  return makeup 
end

function makeupframeReady(scene, elapsedTime,makeup)
  -- ================서버에 등록 할 때 삭제===================
  setParameters()

  makeup:setPathAndBlendMode(KuruMakeupNodeType.BLUSH, BASE_DIRECTORY .. "makeup/img/blush.png", blushBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.CONTOUR, BASE_DIRECTORY .. "makeup/img/contour.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.LIP_COLOR, BASE_DIRECTORY .. "makeup/img/lip_color.png", BlendMode.None)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYE_COLOR, BASE_DIRECTORY .. "makeup/img/eye_color_0001/eye_color_0001.gpb", eyeColorBlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYEBROWS, BASE_DIRECTORY .. "makeup/img/eyebrows.png", eyeBrowsBlendMode)

  --eyeShadow

  if oldEyeShadowLayer1BlendMode ~= eyeShadowLayer1BlendMode then
    oldEyeShadowLayer1BlendMode = eyeShadowLayer1BlendMode
    makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode + 1)
  end

  if oldEyeShadowLayer2BlendMode ~= eyeShadowLayer2BlendMode then
    oldEyeShadowLayer2BlendMode = eyeShadowLayer2BlendMode
    makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode + 1)
  end
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER0, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L0.png", eyeShadowLayer0BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER1, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L1.png", eyeShadowLayer1BlendMode)
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYESHADOW_LAYER2, BASE_DIRECTORY .. "makeup/img/eye_shadow/eye_shadow_L2.png", eyeShadowLayer2BlendMode)

  --eyeLiner
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELINER, BASE_DIRECTORY .. "makeup/img/eye_liner/eye_liner.png", eyeLinerBlendMode)

  --eyeLashes
  makeup:setPathAndBlendMode(KuruMakeupNodeType.EYELASHES, BASE_DIRECTORY .. "makeup/img/eyelashes/eyelashes.png", eyeLashesBlendMode)

  -- ================서버에 등록 할 때 삭제 끝===================
  local strength = 1.0
  local calculatedStrength = (maxStrength - minStrength) * strength + minStrength

  local calculatedLipLayer = lipLayer * calculatedStrength

  -- reBuildLipLayers(scene)
  -- for i=1, 3 do
  --   lipLayers[i]:setEnabled(calculatedLipLayer > 0.0)
  --   lipLayers[i]:setModulateAlpha(calculatedLipLayer)
  -- end

  -- beauty
  makeup_param.laughLine = laughLine * calculatedStrength
  makeup_param.eyeLut = eyeLut * calculatedStrength
  makeup_param.eyePlim = eyePlim * calculatedStrength
  makeup_param.darkCircle = darkCircle * calculatedStrength
  makeup_param.whiteTeeth = whiteTeeth * calculatedStrength
  -- makeup
  makeup_param.faceContour = faceContour * calculatedStrength
  makeup_param.cheek = cheek * calculatedStrength
  makeup_param.lip = lip * calculatedStrength
  makeup_param.colorLens = colorLens * calculatedStrength
  makeup_param.eyeBrow = eyeBrow * calculatedStrength
  makeup_param.eyeShadow = eyeShadow * calculatedStrength
  makeup_param.eyeLiner = eyeLiner * calculatedStrength
  makeup_param.eyeLashes = eyeLashes * calculatedStrength
return makeup 
end

function reBuildLipLayers(scene)

  lipLayerPaths = {"makeup/img/lip_layer/lip_layer0.png", "makeup/img/lip_layer/lip_layer1.png", "makeup/img/lip_layer/lip_layer2.png"}
  curBlendModes = {lipLayer0BlendMode, lipLayer1BlendMode, lipLayer2BlendMode}

  local lipChanged = false
  for i=1, 3 do
    if nil == lipLayers[i] or oldLipLayerBlendModes[i] ~= curBlendModes[i] then
      if lipLayers[i] ~= nil then
        scene:removeNode(lipLayers[i])
      end

      lipLayers[i] = KaleFaceSkinNode.create(
      KaleFaceSkinNodeBuilder.create()
      :path(BASE_DIRECTORY .. lipLayerPaths[i])
      :skinType(KaleFaceSkinType.FACE_EX)
      :blendmode(curBlendModes[i])
      :build())
      if(nil ~= lipLayers[i]) then
        scene:addNodeAndRelease(lipLayers[i])
      end
      oldLipLayerBlendModes[i] = curBlendModes[i]
    
      lipChanged = true
    end
  end

  if lipChanged == true then
    local oldParam = meshDistortionNode:getParam()

    scene:removeNode(meshDistortionNode)
    scene:removeNode(distortionNode)
    if meshContourNode ~= nil then
      scene:removeNode(meshContourNode)
      meshContourNode = nil
    end

    -- meshDistortionNode = KuruMeshDistortionNode.create()
    -- meshDistortionNode:loadDistortionFromString(SceneConfig.instance().distortionJson)
    -- scene:addNodeAndRelease(meshDistortionNode)

    local param = meshDistortionNode:getParam()
    param.eyeSpan = oldParam.eyeSpan
    param.eyeCorner = oldParam.eyeCorner
    param.eyeSize = oldParam.eyeSize
    param.mouthSize = oldParam.mouthSize
    param.lipsPlump = oldParam.lipsPlump
    param.noseNarrow = oldParam.noseNarrow
    param.noseAlar = oldParam.noseAlar
    param.noseLength = oldParam.noseLength
    param.brow = oldParam.brow

    distortionNode = KaleFaceDistortionNode.create()
    distortionNode:setMeshDistortionNode(meshDistortionNode)
    distortionNode:loadDistortionFromString(SceneConfig.instance().distortionJson)
    scene:addNodeAndRelease(distortionNode)

    -- if PropertyConfig.instance():getBoolean("meshContourOn", false) then
    --   meshContourNode = KuruMeshContourDistortionNode.create()
    --   meshContourNode:setMeshDistortionNode(meshDistortionNode)
    --   meshContourNode:loadDistortionFromString(PropertyConfig.instance():getString("meshContourJson", ""))
    --   scene:addNodeAndRelease(meshContourNode)
    -- end
  end

  --  d1 = KaleFaceDistortionNode.createWithPath(BASE_DIRECTORY .. "makeup/d.json")
  --  scene:addNodeAndRelease(d1)
end
